package com.lm;

import java.awt.datatransfer.Transferable;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.TooManyListenersException;
import javax.swing.DropMode;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.TransferHandler;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import mapgenerator.Main;

/**
 *
 * @author Sajarwo Anggai
 */
public class LMControlPanel implements ActionListener, AdjustmentListener, MouseListener {

    public Main comp;
    private TableModel tableModel;
    private JTable table;
    private LayerContainer layerContainer;
    private JPopupMenu LMPopupMenu;
    private JMenuItem MoveUp;
    private JMenuItem MoveDown;
    private JMenuItem Remove;
    private JMenuItem AddNew;
    private int scrollPaneX;
    private int scrollPaneY;
    private int scrollPaneWidth;
    private int scrollPaneHeight;
    public GridWorkspace gridWorkspace;
    private WorkSpace workspace;

    public LMControlPanel(Main comp) {
        this.comp = comp;
        layerContainer = comp.layerContainer;
        gridWorkspace = new GridWorkspace(comp);
        comp.WorkspaceRender.add(gridWorkspace);
        workspace = new WorkSpace(comp);
        comp.WorkspaceRender.addMouseMotionListener(workspace);
        comp.WorkspaceRender.addMouseListener(workspace);
        initPosition();        

        LMPopupMenu = new JPopupMenu();
        AddNew = new JMenuItem("New Layer");
        LMPopupMenu.add(AddNew);
        AddNew.addActionListener(this);
        Remove = new JMenuItem("Remove Layer");
        LMPopupMenu.add(Remove);
        Remove.addActionListener(this);
        MoveUp = new JMenuItem("Move Up");
        LMPopupMenu.add(MoveUp);
        MoveUp.addActionListener(this);
        MoveDown = new JMenuItem("Move Down");
        LMPopupMenu.add(MoveDown);
        MoveDown.addActionListener(this);


        tableModel = new AbstractTableModel() {

            String col[] = {"Visible", "Layer Name"};

            public int getColumnCount() {
                return col.length;
            }

            public int getRowCount() {
                return layerContainer.getLayers().size();
            }

            public Object getValueAt(int row, int col) {
                Layer lc = (Layer) layerContainer.getLayers().get(row);
                return (col == 0) ? lc.isShow() : lc.getName();
            }

            public boolean isCellEditable(int row, int col) {
                return true;
            }

            public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
                Layer lc = (Layer) layerContainer.getLayers().get(rowIndex);
                if (columnIndex == 0) {
                    lc.setShow(lc.isShow() ? false : true);
                    table.repaint();
                    table.revalidate();
                    gridWorkspace.drawLayer();
                } else if (!layerContainer.getLayersMapping().contains(aValue.toString().trim())) {
                    layerContainer.getLayersMapping().set(layerContainer.getLayersMapping().indexOf(lc.getName()), aValue.toString().trim());
                    lc.setName(aValue.toString().trim());
                }
            }

            public Class getColumnClass(int columnIndex) {
                return (columnIndex == 0) ? Boolean.class : String.class;
            }

            public String getColumnName(int column) {
                return col[column];
            }


        };
        table = new JTable(tableModel);
        table.setComponentPopupMenu(LMPopupMenu);
        table.getColumnModel().getColumn(0).setMaxWidth(50);

        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        comp.ScrollPanePanelLayerManager.setViewportView(getTable());
        comp.ScrollPanePanelLayerManager.setComponentPopupMenu(LMPopupMenu);
        table.addMouseListener(this);

        comp.ScrollPaneWorkspaceRender.getHorizontalScrollBar().addAdjustmentListener(this);
        comp.ScrollPaneWorkspaceRender.getVerticalScrollBar().addAdjustmentListener(this);
    }

    public JTable getTable() {
        return table;
    }

    public void actionPerformed(ActionEvent e) {
        if (!comp.project.isCreated()) {
            // return;
        }
        Object ob = e.getSource();
        int indexRow = table.getSelectedRow();
        if (ob == AddNew) {
            for (int loop = 0; loop < 1; loop++) {
                int i = 1;
                while (true) {
                    String layerName = "Layer " + i;
                    if (layerContainer.addLayer(layerName, true)!=null) {
                        break;
                    }
                    i++;
                }
            }
        } else if (ob == Remove) {
            try {
                layerContainer.getLayers().remove(indexRow);
                layerContainer.getLayersMapping().remove(indexRow);
            } catch (Exception err) {
            }
        } else if (ob == MoveUp) {
            if (indexRow != -1 && indexRow != 0) {
                Object tmp = layerContainer.getLayers().get(indexRow);
                layerContainer.getLayers().set(indexRow, layerContainer.getLayers().get(indexRow - 1));
                layerContainer.getLayers().set(indexRow - 1, tmp);
                table.setColumnSelectionInterval(0, 1);
                table.setRowSelectionInterval(indexRow - 1, indexRow - 1);
            }
        } else if (ob == MoveDown) {
            if (indexRow != -1 && indexRow != layerContainer.getLayers().size() - 1) {
                Object tmp = layerContainer.getLayers().get(indexRow);
                layerContainer.getLayers().set(indexRow, layerContainer.getLayers().get(indexRow + 1));
                layerContainer.getLayers().set(indexRow + 1, tmp);
                table.setColumnSelectionInterval(0, 1);
                table.setRowSelectionInterval(indexRow + 1, indexRow + 1);
            }
        }
        table.repaint();
        table.revalidate();
        gridWorkspace.drawLayer();
        this.comp.ScrollPaneWorkspaceRender.repaint();
    }

    @Override
    public void adjustmentValueChanged(AdjustmentEvent e) {
        initPosition();
        System.gc();
    }
    int pttX, pttY, pttW, pttH;

    public void initPosition() {
        scrollPaneX = comp.ScrollPaneWorkspaceRender.getHorizontalScrollBar().getValue();
        scrollPaneY = comp.ScrollPaneWorkspaceRender.getVerticalScrollBar().getValue();
        scrollPaneWidth = scrollPaneX + comp.ScrollPaneWorkspaceRender.getVisibleRect().width;
        scrollPaneHeight = scrollPaneY + comp.ScrollPaneWorkspaceRender.getVisibleRect().height;

        pttX = WorkSpace.pixelsToTiles(scrollPaneX, comp.project.getTileWidth());
        pttY = WorkSpace.pixelsToTiles(scrollPaneY, comp.project.getTileHeight());
        pttW = WorkSpace.pixelsToTiles(scrollPaneWidth, comp.project.getTileWidth());
        pttH = WorkSpace.pixelsToTiles(scrollPaneHeight, comp.project.getTileHeight());

        gridWorkspace.interupt();
        gridWorkspace.setPointArea(pttX, pttY, pttW, pttH);
        scrollPaneX = scrollPaneX - (scrollPaneX % comp.project.getTileWidth());
        scrollPaneY = scrollPaneY - (scrollPaneY % comp.project.getTileHeight());
        gridWorkspace.setBounds(scrollPaneX, scrollPaneY, scrollPaneWidth, scrollPaneHeight);
        gridWorkspace.drawGrid();
        gridWorkspace.drawLayer();
        this.comp.ScrollPaneWorkspaceRender.repaint();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        int indexRow = table.getSelectedRow();
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void drawLayerManager(Layer lm) {
    }
}